package com.jsmtr.rms.controller.DUserResume;

import com.jsmtr.rms.entity.BaseUser;
import com.jsmtr.rms.entity.DUserResume;
import com.jsmtr.rms.entity.ProjectInfoFlow;
import com.jsmtr.rms.service.IDoResumeService;
import com.jsmtr.rms.service.IProjectService;
import com.jsmtr.rms.service.IUtilService;
import com.jsmtr.rms.utils.EnumTools;
import com.jsmtr.rms.utils.StringTools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 应聘者流转到下一步控制
 * @author: cyx
 * @description:
 * @date: Created in 2019/2/25 16:04
 * @modified by:
 */
@Controller
public class DUserResumeNextFlowController {
   @Resource
   private IUtilService utilService;

   @Resource
   private IDoResumeService doResumeService;

   @Resource
   private IProjectService projectService;

    @RequestMapping(value = "/DUserResume/NextAction", method = RequestMethod.POST)
    public void NextAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String[] ids = request.getParameterValues("id");//人员信息ID
            if (ids != null) {
                String pid = request.getParameter("pid").trim();//招聘方案ID
                String fid = request.getParameter("fid").trim();//招聘流程ID
                String deptcode = request.getParameter("deptcode").trim();//责任部门编码
                BaseUser usr = utilService.getSessionUser(request);
                if (usr.getUdept().equals(deptcode)) {
                    //流程是否结束
                    if (utilService.FlowIsDoing(fid)) {
                        List editlist = new ArrayList();
                        List<DUserResume> resumes = doResumeService.getResumes(ids);
                        ProjectInfoFlow nowFlow=projectService.getFolwById(fid);
                        ProjectInfoFlow nextFlow = projectService.getNextFlow(pid, deptcode, fid);
                        String nowflowName=nowFlow.getFname();
                        if(!StringTools.trimNotEmpty(nowflowName)){
                            nowflowName=nowFlow.getNickname();
                        }
                        for (DUserResume user : resumes) {
                            if (user.getFid().equals(fid)&& user.getFqstate().equals("未放弃")) {
                                if (nowflowName.equalsIgnoreCase("面试")||nowflowName.equalsIgnoreCase("面试")) {//面试环节可以直接将进入下一步
                                    user.setFid(nowFlow.getId());
                                    user.setPath(user.getPath() + nowFlow.getId() + "/");

                                }else {
                                    if (user.getState().equals("已通过") || user.getState().equalsIgnoreCase("通过") || user.getState().equalsIgnoreCase("合格") || user.getState().equalsIgnoreCase("已合格")||user.getState().equals("已签约") ||user.getState().equals("无需签约") ) {
                                        user.setFid(nowFlow.getId());
                                        user.setPath(user.getPath() + nowFlow.getId() + "/");
                                    }
                                }
                                editlist.add(user);
                            }
                        }
                        if (editlist.size() > 0) {
                            nowFlow.setState(EnumTools.PlanFlowState.进行中.ordinal());
                            editlist.add(nowFlow);
                            utilService.editList(editlist);
                            utilService.printJsonResult(true, "操作成功！", "no", response);
                        } else {
                            utilService.printJsonResult(false, "当前选中的信息不能执行此操作！", "no", response);
                        }
                    } else {
                        utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
                }
            } else {
                utilService.printJsonResult(false, "请选择人员信息！", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }

}
